package cn.liontalk.concurrent.entity;

import cn.liontalk.concurrent.common.Punishment;

public class StudentRunnable implements Runnable {


    private String name;

    private Punishment punishment;


    public StudentRunnable(String name, Punishment punishment) {
        this.name = name;
        this.punishment = punishment;
    }


    @Override
    public void run() {
        currentCopyWord();
    }

    public void copyWord() {
        int count = 0;
        String threadName = Thread.currentThread().getName();
        while (true) {
            if (punishment.getLeftCopyCount() > 0) {
                int leftCopyCount = punishment.getLeftCopyCount();
                System.out.println(threadName + "线程==>>" + name + "抄写" + punishment.getLeftCopyCount()
                        + ",还剩余===>" + (--leftCopyCount) + "次");
                punishment.setLeftCopyCount(leftCopyCount);
                count++;
            } else {
                break;
            }
        }
        System.out.println(threadName + "线程-" + name + "一共抄写了" + count + "次！");
    }


    public void currentCopyWord() {
        int count = 0;
        String threadName = Thread.currentThread().getName();
        while (true) {
            if (punishment.getLeftCopyCount() > 0) {
                int leftCopyWord = punishment.getLeftCopyCount();
                leftCopyWord--;
                if (leftCopyWord < punishment.getLeftCopyCount()) {
                    punishment.setLeftCopyCount(leftCopyWord);
                }
                System.out.println(threadName + "线程-" + name + "抄写" + punishment.getWordToCopy() + "。还要抄写" + leftCopyWord + "次");
                count++;
            } else {
                break;
            }
        }
        System.out.println(threadName + "线程-" + name + "一共抄写了" + count + "次！");
    }
}
